﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CopySniffer.UI.Core.Views;
using CopySniffer.UI.Core.Core;
using System.IO;
using CopySniffer.UI.Core.Properties;
using CopySniffer.UI.Views;
using CopySniffer.FileLoad;

namespace CopySniffer.UI.Core.Controlers
{
    public class MainControler : BaseControler<IMainView, MainControler>
    {
        private int editorCount;

        public virtual void CreateFile()
        {
            EditorControler editorControler = ControlerFactories.Default.CreateControler<EditorControler>();
            editorControler.Start(this, editorCount++);
            editorControler.Closed += new EventHandler(editorControler_Closed);
            View.DockEditor(editorControler.View);
            editorCount++;
        }

        void editorControler_Closed(object sender, EventArgs e)
        {
            editorCount--;
            EditorControler editor = (EditorControler)sender;
            View.RemoveEditor(editor);
            if (editorCount == 0)
            {
                //View.State = MainView
            }
        }

        public virtual void LoadFile(string fileName)
        {
            if (File.Exists(fileName))
            {
                EditorControler editorControler = ControlerFactories.Default.CreateControler<EditorControler>();
                if (editorControler.Start(this, fileName))
                {
                    View.DockEditor(editorControler.View);
                    editorCount++;
                }
            }
            else
            {
                View.ShowError(
                    String.Format(Resources.CouldNotLocateFile, fileName));
            }
        }

        public void Start()
        {
            DoStart();
        }

        public void SearchCopies(EditorControler activeEditorControler)
        {
            activeEditorControler.SearchCopies();
        }

        public void ShowAboutBox()
        {
            AboutControler aboutControler = new AboutControler();
            aboutControler.Start();
        }

        const string FilterFormat = "{0} ({1})|{1}";
        const string ExtensionFormat = "*.{0}";
        const string FilterSeparator = "|";
        const string ExtensionSeparator = ";";

        public string GetOpenFileFilter()
        {
            StringBuilder filterBuilder = new StringBuilder();
            StringBuilder extensionBuilder = new StringBuilder();
            foreach (IFileLoader loader in FileLoaders.All)
            {
                if (filterBuilder.Length > 0)
                {
                    filterBuilder.Append(FilterSeparator);
                }

                foreach (string extension in loader.SupportedExtensions)
                {
                    if (extensionBuilder.Length > 0)
                    {
                        extensionBuilder.Append(ExtensionSeparator);
                    }

                    extensionBuilder.AppendFormat(ExtensionFormat, extension);
                }

                filterBuilder.AppendFormat(FilterFormat, loader.TypeAlias, extensionBuilder);

                extensionBuilder.Remove(0, extensionBuilder.Length);
            }

            return filterBuilder.ToString();
        }

        public void TreatUnhandledException(Exception exception)
        {
            View.ShowException(
                String.Format(
                    Resources.UnexpectedError,
                    Resources.AppUrl
                    ),
                    exception);

            IApplicationService appService = ServiceLocator.GetService<IApplicationService>();
            appService.Abort();
        }
    }
}
